package com.module.lemlin.mode

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.module.lemlin.base.BaseAbstractFragment
import com.module.lemlin.base.BaseApplication
import com.module.lemlin.base.BaseViewModel

abstract class ViewModeFragment<ViewModel : BaseViewModel> : BaseAbstractFragment() {

    lateinit var viewModel: ViewModel

    abstract fun viewModel(): ViewModel

    override fun doBeforeInit() {
        super.doBeforeInit()
        viewModel = viewModel()
        initViewMode()
    }

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

    /**
     * 生成ViewModel的方法
     */
    inline fun <reified VM : BaseViewModel> createViewModel(
        owner: ViewModelStoreOwner = this
    ): VM = ViewModelProvider(
        owner, ViewModelProvider.AndroidViewModelFactory(BaseApplication.INSTANCE)
    ).get(VM::class.java)


}